package com.health_predict_springboot.controller;

import com.health_predict_springboot.pojo.HealthRisk;
import com.health_predict_springboot.service.HealthRiskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/health-risk")
@Tag(name = "健康风险预测", description = "健康风险预测相关接口")
public class HealthRiskController {

    @Autowired
    private HealthRiskService healthRiskService;

    @Operation(summary = "根据ID查询健康风险", description = "通过风险ID获取健康风险详情")
    @GetMapping("/getById")
    public HealthRisk getRiskById(@RequestParam Long riskId) {
        return healthRiskService.getRiskById(riskId);
    }

    @Operation(summary = "根据指标ID查询健康风险", description = "通过指标ID获取对应的健康风险")
    @GetMapping("/getByIndicatorId")
    public HealthRisk getRiskByIndicatorId(@RequestParam Long indicatorId) {
        return healthRiskService.getRiskByIndicatorId(indicatorId);
    }

    @Operation(summary = "根据用户ID查询健康风险", description = "通过用户ID获取该用户的所有健康风险记录")
    @GetMapping("/getByUserId")
    public List<HealthRisk> getRisksByUserId(@RequestParam Long userId) {
        return healthRiskService.getRisksByUserId(userId);
    }

    @Operation(summary = "获取所有健康风险", description = "获取系统中所有的健康风险记录")
    @GetMapping("/getAll")
    public List<HealthRisk> getAllRisks() {
        return healthRiskService.getAllRisks();
    }

    @Operation(summary = "分页查询健康风险", description = "分页获取健康风险记录")
    @GetMapping("/getByPage")
    public List<HealthRisk> getRisksByPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("pageSize", pageSize);
        return healthRiskService.getRisksByPage(params);
    }

    @Operation(summary = "添加健康风险", description = "新增一条健康风险记录")
    @PostMapping("/add")
    public boolean addRisk(@RequestBody HealthRisk healthRisk) {
        // 设置创建时间和更新时间
        Date now = new Date();
        healthRisk.setCreateTime(now);
        healthRisk.setUpdateTime(now);
        return healthRiskService.addRisk(healthRisk) > 0;
    }

    @Operation(summary = "更新健康风险", description = "更新已有的健康风险记录")
    @PutMapping("/update")
    public boolean updateRisk(@RequestBody HealthRisk healthRisk) {
        // 设置更新时间
        healthRisk.setUpdateTime(new Date());
        return healthRiskService.updateRisk(healthRisk) > 0;
    }

    @Operation(summary = "删除健康风险", description = "通过ID删除健康风险记录")
    @DeleteMapping("/delete")
    public boolean deleteRisk(@RequestParam Long riskId) {
        return healthRiskService.deleteRisk(riskId) > 0;
    }

    @Operation(summary = "按风险等级查询", description = "获取指定风险等级的健康风险记录")
    @GetMapping("/getByRiskLevel")
    public List<HealthRisk> getRisksByRiskLevel(@RequestParam String riskLevel) {
        return healthRiskService.getRisksByRiskLevel(riskLevel);
    }

    @Operation(summary = "预测心血管风险", description = "根据用户健康数据预测心血管疾病风险")
    @PostMapping("/predictCardioRisk")
    public HealthRisk predictCardioRisk(@RequestBody Map<String, Object> healthData) {
        // 从请求体中获取indicatorId
        Long indicatorId = ((Number) healthData.get("indicatorId")).longValue();
        return healthRiskService.predictCardioRisk(indicatorId);
    }

    @Operation(summary = "生成健康改善建议", description = "根据用户健康风险生成个性化改善建议")
    @GetMapping("/generateSuggestions")
    public String generateImprovementSuggest(@RequestParam Long userId) {
        // 找到用户最新的健康风险记录
        List<HealthRisk> risks = healthRiskService.getRisksByUserId(userId);
        if (risks != null && !risks.isEmpty()) {
            return healthRiskService.generateImprovementSuggest(risks.get(0));
        }
        return "暂无健康风险记录，无法生成改善建议";
    }
}